package com.joel.functionsImpl;

import com.joel.crm.customer.Customer;
import com.joel.crm.model.DoubleLinkedlist;
import com.joel.crm.model.Node;
import com.joel.interfaces.Functions;

;

public class FunctionImpl implements Functions{
	DoubleLinkedlist doubleLinkedlist;
	
	private Node head;
	private Node cursorNode;
	public FunctionImpl(DoubleLinkedlist doubleLinkedlist){
		this.doubleLinkedlist =  doubleLinkedlist;
		head = doubleLinkedlist.getHead();
		
	}

	@Override
	public void add(String[] com2) {
		
		Customer customer = new Customer();
		for (int i = 0; i < com2.length; i++) {
			String[] map = new String[2];
			map = com2[i].split(":");

			switch (map[0]) {
			case "id":
				customer.setId(map[1]);
				break;
			case "name":
				customer.setName(map[1]);
				break;
			case "sex":
				customer.setSex(map[1]);
				break;
			case "birth":
				customer.setBirth(map[1]);
				break;
			case "address":
				customer.setAddress(map[1]);
				break;
			case "phone":
				customer.setPhone(map[1]);
				break;
			}
		}
		doubleLinkedlist.add(customer);
		
	}

	@Override
	public void delete(String[] com2) {
		cursorNode = head;
		while (cursorNode != null) {
			for (int i = 0; i < com2.length; i++) {
				String[] map = new String[2];
				map = com2[i].split(":");
				if (map[1].equals(cursorNode.getData().getId())) {
					System.out.println("the record need delete:  "
							+ cursorNode.getData().getId() + " "
							+ cursorNode.getData().getName() + " "
							+ cursorNode.getData().getSex() + " "
							+ cursorNode.getData().getBirth() + " "
							+ cursorNode.getData().getAddress() + " "
							+ cursorNode.getData().getPhone()
							+ "\nyou can see the result by L\n");
					doubleLinkedlist.delete(cursorNode);
					break;
				}
			}
			cursorNode = cursorNode.getNextNode();
		}
	}

	@Override
	public void update(String[] com2) {
		cursorNode = head;
		boolean flag = false;
		while (cursorNode != null) {
			Customer customer = cursorNode.getData();
			for (int i = 0; i < com2.length; i++) {
				String[] map = new String[2];
				map = com2[i].split(":");
				switch (map[0]) {
				case "id":
					if (map[1].equals(cursorNode.getData().getId())) {
						flag = true;
					} 
					break;
				case "name":
					if(flag){
					customer.setName(map[1]);
					}
					break;
				case "sex":
					if(flag){
					customer.setSex(map[1]);
					}
					break;
				case "birth":
					if(flag){
					customer.setBirth(map[1]);
					}
					break;
				case "address":
					if(flag) {
					customer.setAddress(map[1]);
					}
					break;
				case "phone":
					if (flag) {
					customer.setPhone(map[1]);
					}
					break;
				}
			}
			if (flag) {
				System.out.println("the record was changed to:  " + cursorNode.getData().getId()
						+ " " + cursorNode.getData().getName() + " "
						+ cursorNode.getData().getSex() + " "
						+ cursorNode.getData().getBirth() + " "
						+ cursorNode.getData().getAddress() + " "
						+ cursorNode.getData().getPhone());
				break;
			}
			cursorNode = cursorNode.getNextNode();
		}
		
	}

	@Override
	public void list(String[] com2) {
		cursorNode = head;
		boolean flag = false;
		while (cursorNode != null) {
			for (int i = 0; i < com2.length; i++) {
				String[] map = new String[2];
				map = com2[i].split(":");
				switch (map[0]) {
				case "id":
					if (map[1].equals(cursorNode.getData().getId())) {
						flag = true;
					} else {
						flag = false;
						System.out.println("id not found, please input again");
					}
					break;
				case "name":
					if (map[1].equals(cursorNode.getData().getName())) {
						System.out.println("the name match succeed");
						flag = true;
					} else {
						flag = false;
						System.out.println("name not found, please input again");
					}
					break;
				case "sex":
					if (map[1].equals(cursorNode.getData().getSex())) {
						System.out.println("the sex match succeed");
						flag = true;
					} else {
						flag = false;
						System.out.println("sex not found, please input again");
					}
					break;
				case "birth":
					if (map[1].equals(cursorNode.getData().getBirth())) {
						System.out.println("the birth match succeed");
						flag = true;
					} else {
						flag = false;
						System.out.println("birth not found, please input again");
					}
					break;
				case "address":
					if (map[1].equals(cursorNode.getData().getAddress())) {
						System.out.println("the address match succeed");
						flag = true;
					} else {
						flag = false;
						System.out.println("address not found, please input again");
					}
					break;
				case "phone":
					if (map[1].equals(cursorNode.getData().getPhone())) {
						System.out.println("the phone match succeed");
						flag = true;
					} else {
						flag = false;
						System.out.println("phone not found, please input again");
					}
					break;
				}
			}
			if (flag) {
				System.out.println("list result:  " + cursorNode.getData().getId() + " "
						+ cursorNode.getData().getName() + " "
						+ cursorNode.getData().getSex() + " "
						+ cursorNode.getData().getBirth() + " "
						+ cursorNode.getData().getAddress() + " "
						+ cursorNode.getData().getPhone());
				break;// 如果没有break，cursorNode还会往下走
			}
			cursorNode = cursorNode.getNextNode();
		}
		
	}
	public void  sortById(){
		Customer[] arrays = LinkedToArray();
		Customer temp = null;
		
		for(int i=0;i<arrays.length-1;i++){
			for(int j=0;j<arrays.length-1-i;j++){
				if(Integer.parseInt(arrays[j].getId())> Integer.parseInt(arrays[j+1].getId())){
					temp = arrays[j];
					arrays[j] = arrays[j+1];
					arrays[j+1] = temp;
				}
			}
		}
		sortResult(arrays);
	}
	public static void sortResult(Customer[] arrays) {
		for(int i=0; i<arrays.length; i++) { 
			System.out.println(arrays[i]);
		}
	}
	private Customer[] LinkedToArray(){
		Customer[] customers = new Customer[doubleLinkedlist.size()];
		cursorNode = head;
		for(int i = 0;i<doubleLinkedlist.size();i++){
			customers[i] = cursorNode.getData();
			cursorNode = cursorNode.getNextNode();
		}
		return customers;
	}

}
